import utils from "../../utils/index";
import {rankModel,userModel} from "../../apis/index"
const app = getApp()
import regeneratorRuntime from '../../libs/wxPromise.min';
import {
  InterAd
} from '../../utils/InterAd'
Page({
  data: {
    moveArea:[],
    msgData: [
      { name:'XX',avatar:'XX',gift:'xx'},
      { name:'XX',avatar:'XX',gift:'xx'},
      { name:'XX',avatar:'XX',gift:'xx'},
      { name:'XX',avatar:'XX',gift:'xx'},
      { name:'XX',avatar:'XX',gift:'xx'},
    ],
    
    giftsIndex:'-1',
    giftList:[],
    giftTotalPrice:0,
    page: {
      page: 1,
      pageSize: 50
    },
    barrageList: [],
    giftList:[],
    clubIndex:0, //选择粉丝团下标
    clubList:[]
  },
  mixins: [require('../../myMixin.js')],
  async onLoad (options) {
    let node = await utils.createSelectorQuery('.gift-content');
    
    // 宽高
    this.data.moveArea = [[80,node[0].width-200],[100,node[0].height-100]]

    let sid = options.sid?options.sid:''

    console.log(sid)
    if(!sid){
      utils.navigateBack({
        delta:1
      })
      return
    }
    this.setData({
      sid:sid,
      isIOS:app.globalData.isIOS,
      isShow:app.globalData.isShow
    })
    this.getHitTheList(sid)
    this.getUserInfo()
    
  },
  onReady: function () {
    let interAd = new InterAd('7fb828e435af61d59234ae3978c20221')
    interAd.showAd()
  },
  onShow: function () {
    setTimeout(()=>{
      this.getBarrageList()
    },1000)
    
  },
  async getUserInfo(){
    let userInfo =await userModel.getUserInfo()
    this.data.userInfo = userInfo
  },
  getHitTheList(sid){
    rankModel.hitTheList({sid}).then(res=>{
      this.setData({
        info:res,
        clubList:res.list
      })
    })
  },
  showClubList(){
    let {clubList,clubIndex} = this.data
    if(clubList.length>1){
      this.setData({
        clubModal:true
      })
    }
  },
  clubItemTap(e){
    let index = e.currentTarget.dataset.index
    this.setData({
      clubIndex:index,
      clubModal:false
    })
  },
  hideClubModal(){
    this.setData({
      clubModal:false
    })
  },
  getGiftItemTopLeft(){
    let {moveArea} = this.data
    let left = Math.floor(Math.random()*(moveArea[0][1]-moveArea[0][0])+moveArea[0][0])
    let top = Math.floor(Math.random()*(moveArea[1][1]-moveArea[1][0])+moveArea[1][0])
    return {left,top};
  },
  change: function (data) {
    let _this = this
    let colors = ['red','yellow','green','blue','purple','pink']
    let count = colors.length
    return data.map((item, index) => {
      item.time = 1.75;
      let {left,top} = _this.getGiftItemTopLeft()
      item.top = top
      item.left = left
      item.bgColor =colors[parseInt(Math.random()*count)]
      return item
    })
  },
  itemTap(e){
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.id
    let { giftList,giftsIndex } = this.data
    giftList.forEach(el=>{
      el.checked = false
    })
    giftList[index].checked = !giftList[index].checked
    giftsIndex = giftList[index].checked?index:'-1'
    
    this.setData({
      giftsIndex:giftsIndex,
      giftTotalPrice:giftList[index].checked?giftList[index].cost:0,
      giftview:id
    })
  },
  showGiftModal(){
    this.getGiftsList(()=>{
      this.setData({
        modalName:'giftModal'
      })
    })
  },
  hideModal(){
    this.setData({
      modalName:''
    })
  },
  getBarrageList() {  // 弹幕列表
    let {
      page,
      pageSize
    } = this.data.page
    let {
      barrageList,sid
    } = this.data
    let param = {
      page: page,
      pageSize: pageSize,
      sid:sid
    }
    utils.showLoading()
    rankModel.getBarrageList(param).then(res => {
      barrageList = res.list
      this.change(barrageList)
      this.setData({
        barrageList: barrageList
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  getGiftsList(callBack) {  // 礼物列表
    let {
      giftList
    } = this.data
    utils.showLoading()
    rankModel.getGiftList().then(res => {
      giftList = []
      let _list = res.list
      _list.forEach(el => {
        el.checked = false
      });
      giftList = giftList.concat(_list)

      this.setData({
        giftList: giftList
      },()=>{
        callBack && callBack()
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  goUrl(e){
    let url = e.currentTarget.dataset.url
    utils.goUrl(url)
  },
  postGift(e){
    let {giftList,giftsIndex,sid,clubIndex,clubList,userInfo} = this.data
    if(giftsIndex == -1){
      utils.showToast('请选择赠送礼物','none')
      return
    }
    let item = giftList[giftsIndex]
    if(userInfo.star<item.cost){
      utils.showToast('幸运星数据不足','none')
      return
    }
    let fid = clubList.length?clubList[clubIndex].id:''
    let param = {
      sid:sid,
      fid:fid,
      gid:item.id
    }
    utils.showLoading()
    rankModel.postGift(param).then(res=>{
      utils.showToast('幸运星赠送成功','none')
      this.setData({
        giftsIndex:-1
      })
      this.getUserInfo()
      this.getBarrageList()
      this.hideModal()
      utils.hideAll()
    })
  },
  toIndex(){
    utils.reLaunch({
      url:'../rank/index?tabIndex=0'
    })
  }
})